home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / interpolation / spline.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-18  |  4.9 KB  |  182 lines

  1. /* interpolation/spline.c
  2.  * 
  3.  * Copyright (C) 2001 Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <string.h>
  22. #include <gsl/gsl_errno.h>
  23. #include <gsl/gsl_interp.h>
  24. #include <gsl/gsl_spline.h>
  25.  
  26. gsl_spline *
  27. gsl_spline_alloc (const gsl_interp_type * T, size_t size)
  28. {
  29.   gsl_spline * spline = (gsl_spline *) malloc (sizeof(gsl_spline));
  30.   
  31.   if (spline == NULL)
  32.     {
  33.       GSL_ERROR_NULL ("failed to allocate space for spline struct", 
  34.                       GSL_ENOMEM);
  35.     }
  36.   
  37.   spline->interp = gsl_interp_alloc (T, size);
  38.   
  39.   if (spline->interp == NULL)
  40.     {
  41.       free (spline);          
  42.       GSL_ERROR_NULL ("failed to allocate space for interp", GSL_ENOMEM);
  43.     };
  44.     
  45.   spline->x = (double *) malloc (size * sizeof(double));
  46.  
  47.   if (spline->x == NULL)
  48.     {
  49.       gsl_interp_free(spline->interp);
  50.       free(spline);
  51.       GSL_ERROR_NULL ("failed to allocate space for x", GSL_ENOMEM);
  52.     }
  53.  
  54.   spline->y = (double *) malloc (size * sizeof(double));
  55.  
  56.   if (spline->y == NULL)
  57.     {
  58.       free(spline->x);
  59.       gsl_interp_free(spline->interp);
  60.       free(spline);
  61.       GSL_ERROR_NULL ("failed to allocate space for y", GSL_ENOMEM);
  62.     }
  63.   
  64.   spline->size = size;
  65.  
  66.   return spline;
  67. }
  68.  
  69. int
  70. gsl_spline_init (gsl_spline * spline, const double x_array[], const double y_array[], size_t size)
  71. {
  72.   if (size != spline->size)
  73.     {
  74.       GSL_ERROR ("data must match size of spline object", GSL_EINVAL);
  75.     }
  76.   
  77.   memcpy (spline->x, x_array, size * sizeof(double));
  78.   memcpy (spline->y, y_array, size * sizeof(double));
  79.  
  80.   {
  81.     int status = gsl_interp_init (spline->interp, x_array, y_array, size);
  82.     return status;
  83.   }
  84. }
  85.  
  86. void
  87. gsl_spline_free (gsl_spline * spline)
  88. {
  89.   gsl_interp_free (spline->interp);
  90.   free (spline->x);
  91.   free (spline->y);
  92.   free (spline);
  93. }
  94.  
  95. int
  96. gsl_spline_eval_e (const gsl_spline * spline, 
  97.                    double x,
  98.                    gsl_interp_accel * a, double *y)
  99. {
  100.   return gsl_interp_eval_e (spline->interp, 
  101.                             spline->x, spline->y,
  102.                             x, a, y);
  103. }
  104.  
  105. double
  106. gsl_spline_eval (const gsl_spline * spline,
  107.          double x,
  108.          gsl_interp_accel * a)
  109. {
  110.   return gsl_interp_eval (spline->interp, 
  111.                           spline->x, spline->y,
  112.                           x, a);
  113. }
  114.  
  115.  
  116. int
  117. gsl_spline_eval_deriv_e (const gsl_spline * spline,
  118.                          double x,
  119.                          gsl_interp_accel * a,
  120.                          double *dydx)
  121. {
  122.   return gsl_interp_eval_deriv_e (spline->interp, 
  123.                                   spline->x, spline->y,
  124.                                   x, a, dydx);
  125. }
  126.  
  127. double
  128. gsl_spline_eval_deriv (const gsl_spline * spline,
  129.                double x,
  130.                gsl_interp_accel * a)
  131. {
  132.   return gsl_interp_eval_deriv (spline->interp, 
  133.                                 spline->x, spline->y,
  134.                                 x, a);
  135. }
  136.  
  137.  
  138. int
  139. gsl_spline_eval_deriv2_e (const gsl_spline * spline,
  140.                           double x,
  141.                           gsl_interp_accel * a,
  142.                           double * d2)
  143. {
  144.   return gsl_interp_eval_deriv2_e (spline->interp, 
  145.                                    spline->x, spline->y,
  146.                                    x, a, d2);
  147. }
  148.  
  149. double
  150. gsl_spline_eval_deriv2 (const gsl_spline * spline,
  151.                 double x,
  152.                 gsl_interp_accel * a)
  153. {
  154.   return gsl_interp_eval_deriv2 (spline->interp, 
  155.                                  spline->x, spline->y,
  156.                                  x, a);
  157. }
  158.  
  159.  
  160. int
  161. gsl_spline_eval_integ_e (const gsl_spline * spline,
  162.                          double a, double b,
  163.                          gsl_interp_accel * acc,
  164.                          double * result)
  165. {
  166.   return gsl_interp_eval_integ_e (spline->interp, 
  167.                                   spline->x, spline->y,
  168.                                   a, b, acc, result);
  169. }
  170.  
  171.  
  172. double
  173. gsl_spline_eval_integ (const gsl_spline * spline,
  174.                        double a, double b,
  175.                gsl_interp_accel * acc)
  176. {
  177.   return gsl_interp_eval_integ (spline->interp, 
  178.                                 spline->x, spline->y,
  179.                                 a, b, acc);
  180. }
  181.  
  182.